// !#2 定义一个仓库
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';

// 仓库名（唯一就行）、函数（组合式的写法）或对象（选项式的写法）
/* export const useCounterStore = defineStore('counter', () => {
  // 这里面写的代码类似写到组件的 setup 中
  // state
  const count = ref(1)
  // mutation
  const increment = (n: number) => {
    count.value += n
  }
  const decrement = (n: number) => {
    count.value -= n
  }

  // getter
  const doubleCount = computed(() => {
    return count.value * 2
  })

  // action
  const incrementAsync = (n: number) => {
    setTimeout(() => {
      increment(n)
    }, 1000)
  }

  return { count, doubleCount, increment, decrement, incrementAsync }
}) */

export const useCounterStore = defineStore('counter', {
  state: () => ({
    count: 1
  }),
  actions: {
    increment(n: number) {
      this.count += n
    },
    decrement(n: number) {
      this.count -= n
    },
    incrementAsync(n: number){
      setTimeout(() => {
        this.increment(n)
      }, 1000)
    }
  },
  getters: {
    doubleCount: (state) => state.count * 2
  }
})